package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.service.WmchannelService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class WmchannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmchannelService {

    @Autowired
    private WmChannelMapper wmChannelMapper;
    /**
     * 查询所有的频道数据
     *
     * @return
     */
    @Override
    public ResponseResult findAll() {
        List<WmChannel> list = list();
        return ResponseResult.okResult(list);
    }

    //分页查询
    @Override
    public ResponseResult listChannel(ChannelDto dto) {
        if (null == dto) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();

        IPage<WmChannel> page = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmChannel> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(dto.getName())) {
            queryWrapper.like(WmChannel::getName, dto.getName());
        }
        queryWrapper.orderByDesc(WmChannel::getCreatedTime);

        page(page, queryWrapper);

        PageResponseResult pageResponseResult = new PageResponseResult(
                dto.getPage(),
                dto.getSize(),
                (int) page.getTotal());
        pageResponseResult.setData(page.getRecords());
        return pageResponseResult;
    }

    //频道新增
    @Override
    public ResponseResult saveChannel(WmChannel wmChannel) {
        if (!StringUtils.isNotBlank(wmChannel.getName())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        List<WmChannel> list =  new ArrayList<>();
        for (WmChannel channel : list) {
            if(wmChannel.getName().equals(channel.getName())){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
            }
        }


        wmChannel.setIsDefault(true);
        wmChannel.setCreatedTime(new Date());
        save(wmChannel);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult deleteById(@PathVariable Integer id) {
        if(id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        wmChannelMapper.deleteById(id);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult updateChannel(WmChannel wmChannel) {
        if(wmChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmChannel byId = getById(wmChannel.getId());
        if(byId ==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        byId.setStatus(wmChannel.getStatus());
        byId.setName(wmChannel.getName());
        byId.setDescription(wmChannel.getDescription());
        byId.setOrd(wmChannel.getOrd());
        updateById(byId);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
